<template>
	<view>		
		<form>
			<view class="cu-form-group margin-top">
				<view class="title">用户名</view>
				<input placeholder="填写用户名" v-model="user.name"></input>
			</view>
			
			<view class="cu-form-group">
				<view class="title">密码</view>
				<input type="password" placeholder="填写密码" v-model="user.password"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">确认密码</view>
				<input type="password" placeholder="再次输入密码" v-model="rePwd"></input>
			</view>
			
			<view class="cu-form-group">
				<view class="title">手机号码</view>
				<input placeholder="填写手机号码" v-model="user.telephone" ></input>
				<view class="cu-capsule radius">
					<view class='cu-tag bg-blue '>
						+86
					</view>
					<view class="cu-tag line-blue">
						中国大陆
					</view>
				</view>
			</view>
			<!-- 给一个高度 
			<view class="cu-form-group">
				<view class="title">验证码</view>
				<input placeholder="填写发送的验证码" ></input>
				<button class='cu-btn bg-green shadow'>验证码</button>
			</view>
			-->
			
			<radio-group class="block" @change="RadioChange">
				<view class="cu-form-group">
					<view class="title">性别</view>
					<view>
						<radio @click="changeSex('男')" class='blue radio' :class="radio=='C'?'checked':''" :checked="radio=='C'?true:false" value="C">男</radio>
						<radio @click="changeSex('女')" class='red margin-left-sm' :class="radio=='D'?'checked':''" :checked="radio=='D'?true:false" value="D">女</radio>
					</view>
				</view>
			</radio-group>
			
			<view class="cu-bar bg-white margin-top">
				<view class="action">
					选择头像
				</view>
				<view class="action">
					{{imgList.length}}/1
				</view>
			</view>
			<view class="cu-form-group">
				<view class="grid col-4 grid-square flex-sub">
					<view class="bg-img" v-for="(item,index) in imgList" :key="index" @tap="ViewImage" :data-url="imgList[index]">
					 <image :src="imgList[index]" mode="aspectFill"></image>
						<view class="cu-tag bg-red" @tap.stop="DelImg" :data-index="index">
							<text class='cuIcon-close'></text>
						</view>
					</view>
					<view class="solids" @tap="ChooseImage" v-if="imgList.length<1">
						<text class='cuIcon-cameraadd'></text>
					</view>
				</view>
			</view>
		</form>
		<view class="register-commit">
			<button @click="submmit" class='cu-btn bg-green shadow'>提交</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				rePwd:'',
				user:{
					name:'',
					password:'',
					telephone:'',
					sex:'男',
				},
				index: -1,
				multiIndex: [0, 0, 0],
				time: '12:01',
				date: '2018-12-25',
				switchA: false,
				switchB: true,
				switchC: false,
				switchD: false,
				radio: 'A',
				checkbox: [{
					value: 'A',
					checked: true
				}, {
					value: 'B',
					checked: true
				}, {
					value: 'C',
					checked: false
				}],
				imgList: [],
				modalName: null,
				textareaAValue: '',
				textareaBValue: '',
				forbidden_status:0
				
			};
		},
		methods: {
		changeSex(a){
			this.user.sex=a
		},
			PickerChange(e) {
						this.index = e.detail.value
					},
					MultiChange(e) {
						this.multiIndex = e.detail.value
					},
					TimeChange(e) {
						this.time = e.detail.value
					},
					DateChange(e) {
						this.date = e.detail.value
					},
					RegionChange(e) {
						this.region = e.detail.value
					},
					SwitchA(e) {
						this.switchA = e.detail.value
					},
					SwitchB(e) {
						this.switchB = e.detail.value
					},
					SwitchC(e) {
						this.switchC = e.detail.value
					},
					SwitchD(e) {
						this.switchD = e.detail.value
					},
					RadioChange(e) {
						this.radio = e.detail.value
					},
					submmit(){
						if(!this.user.name){
							this.$api.msg('用户名不能为空')
							return
						}
						if(!this.user.password){
							this.$api.msg('请填写密码')
							return
						}
						if(!this.rePwd){
							this.$api.msg('请确认密码')
							return
						}
						if(this.user.password.length<8){
							this.$api.msg('8-18位不含特殊字符的数组，字母组合')
							return
						}
						if(this.user.password!=this.rePwd){
							this.$api.msg('两次密码不一致')
							return
						}
						if(!this.user.telephone){
							this.$api.msg('请填写手机号')
							return
						}
					
						if(!this.imgList[0]){
							this.$api.msg('请选择头像')
							return
						}
						//修改按钮的点击状态，变成不可点
						this.registing = true;
						let filePath = this.imgList[0];
						if(filePath){
							uni.uploadFile({
								url: this.baseurl+'user/regist',
								filePath: filePath,
								name: 'file',
								formData:this.user,
								success: res => {
									console.log(res);
									//let last=JSON.stringify(res)
									let e=JSON.parse(res.data)
									console.log('last',e);
									if (e.code == 200){
									uni.showToast({
										icon:'success',
										title:'注册成功了',
										duration:500,
									})
									setTimeout(()=>{
										uni.navigateTo({
											url:'login-name'
										})
									},500)									
									}else if(e.code == 30001){
									uni.showToast({
										icon:'false',
										title:'用户名不能重复',
										duration:500,
									})
									}else if(e.code == 30002){
									uni.showToast({
										icon:'false',
										title:'手机号不能重复',
										duration:500,
									})
									}										
								},
							})
						}
					},
					CheckboxChange(e) {
						var items = this.checkbox,
							values = e.detail.value;
						for (var i = 0, lenI = items.length; i < lenI; ++i) {
							items[i].checked = false;
							for (var j = 0, lenJ = values.length; j < lenJ; ++j) {
								if (items[i].value == values[j]) {
									items[i].checked = true;
									break
								}
							}
						}
					},
					ChooseImage() {
						uni.chooseImage({
							count: 4, //默认9
							sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
							sourceType: ['album'], //从相册选择
							success: (res) => {
								if (this.imgList.length != 0) {
									this.imgList = this.imgList.concat(res.tempFilePaths)
								} else {
									this.imgList = res.tempFilePaths
								}
							}
						});
					},
					ViewImage(e) {
						uni.previewImage({
							urls: this.imgList,
							current: e.currentTarget.dataset.url
						});
					},
					DelImg(e) {
						uni.showModal({
							title: '头像',
							content: '确定要删除选择的头像么？',
							cancelText: '取消',
							confirmText: '删除',
							success: res => {
								if (res.confirm) {
									this.imgList.splice(e.currentTarget.dataset.index, 1)
								}
							}
						})
					},
					textareaAInput(e) {
						this.textareaAValue = e.detail.value
					},
					textareaBInput(e) {
						this.textareaBValue = e.detail.value
					}
				}
			}
</script>

<style>
	@import url("main.css");
	@import url("icon.css");
	.register-commit {
		position: absolute;
		left: 0;
		bottom: 50upx;
		width: 100%;
		font-size: $font-sm + 2upx;
		color: $font-color-base;
		text-align: center;
		text {
			color: $font-color-spec;
			margin-left: 10upx;
		}
	}
	.cu-form-group .title {
		min-width: calc(4em + 15px) ;
	}
</style>
